var $salveage = $(".salvage");
var interval;
var num;
var prize = new Array();
prize[0] = "salvage_0.png";
prize[1] = 'salvage_1.png';
prize[2] = 'salvage_2.png';
prize[3] = 'salvage_3.png';
prize[4] = 'salvage_4.png';
prize[5] = 'salvage_5.png';
prize[6] = 'salvage_6.png';
prize[7] = 'salvage_7.png';
prize[8] = 'salvage_8.png';
prize[9] = 'salvage_9.png';
prize[10] = 'salvage_10.png';
prize[11] = 'salvage_11.png';
prize[12] = 'salvage_12.png';
prize[15] = 'salvage_15.png';

var isAction = false;
function reset() {

	if (isAction) {
		mui.toast("正在打捞,请勿重复");
		return false;
	};
	
	isAction = true;
	$salveage.css({
		left : "105px",
		top : "450px",
		width : "166px",
		height : "71px"
	});
	
	$(".prize").css({
		"z-index" : 6,
		"display" : "none"
	});

	track.init($('.salvage'), 85, 450, 300);
}

// 抛出网兜 旋转加放大的过程
function salveage() {

	$salveage.animate({
		left : "150px",
		top : "160px",
		width : "266px",
		height : "171px",
	}, 1000, function() {

	});
}
// 来回搜索
function search() {
	$salveage.animate({
		left : "450px",
	}, 1000, function() {
		$salveage.animate({
			left : "150px"
		}, 1000, function() {

			showPrize();
		})
	});
}
function showPrize() {
	$salveage.css({
		"-webkit-transform" : "rotate(0deg)"
	});
	$(".sea").css({
		"z-index" : -1
	});
	$(".water").css({
		"z-index" : 3,
		"opacity" : 1,
		"top" : "150px"
	});
	$(".water").animate({
		top : "220px",
		opacity : 0
	}, 500, function() {
	});

	getPrize();
	$(".prize").css({
		"z-index" : 1,
		"display" : "block"
	});

	$(".sea").css({
		"z-index" : 1
	});
	isAction = false;
}

function getPrize() {
	
	var index = Math.floor((Math.random()*prize.length))
	
	$("#prize").attr("src", "/images/bottle/" + prize[index]);
}

var track = {
	interval : 0,// 定时器
	offsetX : 100,// 初始坐标x
	offsetY : 100,// 初始坐标Y
	$box : 0,// 选定的对象
	angle : 0,// 初始角度
	R : 100,// 半径
	w : 0,
	h : 0,
	rotate : 0,
	jump : function() {
		if (track.angle <= -120) {
			clearInterval(track.interval);
			track.$box.addClass("rotate");
			$(".sea").css({
				"z-index" : 6
			});
			search();
		}

		// 快掉落的时候 插入海底
		if (track.angle < -100) {
			$(".sea").css({
				"z-index" : 6
			});

		}

		track.w += 0.5;
		track.h += 0.5;
		track.rotate -= 0.7;
		track.angle -= 1;
		var rotate = "rotate(" + track.rotate + "deg)";
		var x = track.offsetX + track.R * Math.cos(track.angle * Math.PI / 180);
		var y = track.offsetY + track.R * Math.sin(track.angle * Math.PI / 180);
		track.$box.css({
			left : x,
			top : y,
			width : track.w,
			height : track.h,
			"-webkit-transform" : rotate
		});

	},
	init : function(obj, x, y, r) {
		track.angle = 0;
		track.rotate = 0;
		track.$box = obj;
		track.w = obj.width();
		track.h = obj.height();
		track.offsetX = x;
		track.offsetY = y;
		track.R = r;
		track.interval = setInterval(track.jump, 10);
	}
}